<?php
include_once '../config/koneksi.php';



class LiburCutiBersama{
var $id_lcb;
var $thn_cuti;
var $tgl_lcb;
var $ket_lcb;
var $jns_lcb;
var $status_lcb;
protected $connect;

    function __construct(){
        $this->connect = new Koneksi();
        $this->connect->connect();
        
    }

    //fungsi untuk merekam data libur dan cuti bersama
    function insertLCB($thn_lcb,$tgl_lcb,$ket_lcb,$jns_lcb){
        $sql=$this->connect->prepare("INSERT INTO t_lcb(thn_cuti,tgl_lcb,ket_lcb,jns_lcb,status_lcb) VALUES(?,?,?,?,?)");
        $isi=array($thn_lcb,$tgl_lcb,$ket_lcb,$jns_lcb,1);
        $sql->execute($isi);
        $hasil=$sql->rowCount();
        return $hasil;
    }
    
    
    //untuk mengambil seluruh data libur dan cuti bersama
    function getAllLCB($thn_lcb){
        $data_lcb=array();
        $sql=$this->connect->query("select a.id_lcb,thn_cuti,DATE_FORMAT(a.tgl_lcb,'%d-%m-%Y') as tgl, a.ket_lcb,a.jns_lcb,CONCAT(a.status_lcb,',',b.ket_status) as ket_status from t_lcb as a INNER JOIN ref_statuslcb as b
                                    ON a.status_lcb=b.status_lcb
                                    where thn_cuti='".$thn_lcb."' ORDER BY tgl_lcb ASC");
        if($sql){
            while ($row=$sql->fetch()){
                $lcb= new LiburCutiBersama();
                $lcb->id_lcb=$row['id_lcb'];
                $lcb->thn_cuti=$row['thn_cuti'];
                $lcb->tgl_lcb=$row['tgl'];
                $lcb->ket_lcb=$row['ket_lcb'];
                $lcb->jns_lcb=$row['jns_lcb'];
                $lcb->status_lcb=$row['ket_status'];
                $data_lcb[]=$lcb;
            }
            return $data_lcb;
        }
    }

    //untuk menghapus cuti bersama sesuai dengan data yang dipilih user
    function deleteLCB($id_lcb){
        $sql=$this->connect->prepare("delete from t_lcb where id_lcb= ?");
        $isi=array($id_lcb);
        $sql->execute($isi);
        $hasil=$sql->rowCount();
        return $hasil;
    }

    
    
    //untuk mendapatkan 1 data cuti bersama yang dipilih user  
    function countCLB($tgl_awal,$tgl_akhir){
            $sql=$this->connect->query("select * from t_lcb WHERE jns_lcb='CB' AND tgl_lcb BETWEEN '$tgl_awal' AND '$tgl_akhir'");
            return $sql->rowCount();
    }

    //untuk mengubah 1 data cuti bersama yang dipilih oleh user
    function updateLCB($id,$thn_cuti,$tgl_lcb,$ket_lcb,$jns_lcb){
        $this->id_lcb=$id;
        $this->thn_cuti=$thn_cuti;
        $this->tgl_lcb=$tgl_lcb;
        $this->ket_lcb=$ket_lcb;
        $this->jns_lcb=$jns_lcb;
        $sql=$this->connect->prepare("update t_lcb set thn_cuti= ?,tgl_lcb= ?,ket_lcb= ?,jns_lcb= ? WHERE id_lcb= ?");
        $isi=array($thn_cuti,$tgl_lcb,$ket_lcb,$jns_lcb,$id);
        $sql->execute($isi);
        $hasil=$sql->rowCount();
        var_dump($hasil);
        return $hasil;
    }

    //untuk mendapatkan tanggal cuti bersama dari database
    //digunakan untuk melakukan validasi pada saat perekaman, apakah pada tanggal tersebut telah direkam cuti bersama
    function getTanggal($tgl_lcb){
        $sql=$this->connect->query("select * from t_lcb where DATE_FORMAT(tgl_lcb,'%d-%m-%Y')=".$tgl_lcb);
        var_dump($sql);
        $hasil=$sql->rowCount();
        return $hasil;
    }
    
     function getTanggalLCB($id_lcb){
        $sql=$this->connect->query("select id_lcb,DATE_FORMAT(tgl_lcb,'%d-%m-%Y') as tgl_lcb,ket_lcb,jns_lcb from t_lcb where id_lcb=".$id_lcb);
        if($sql){
            $libur=new LiburCutiBersama();
            $row=$sql->fetch();
                $libur->id_lcb=$row['id_lcb'];
                $libur->tgl_lcb=$row['tgl_lcb'];
                $libur->ket_lcb=$row['ket_lcb'];
                $libur->jns_lcb=$row['jns_lcb'];
                return $libur;
            }
        }
        
        function setujuiLCB($id_lcb){
             $sql=$this->connect->query("update t_lcb set status_lcb=2 where id_lcb=".$id_lcb);
             if($sql)return true;                 
        }
}